import java.util.Scanner;

public class Main {


    private static final Scanner scanner = new Scanner(System.in);

    private static String dataDir;

    static {
        String os = System.getProperty("os.name");
        System.out.println(os);

        if (os.startsWith("Windows")) {
            dataDir = "D:\\ds\\data\\";
        } else {
            dataDir = "/opt/ds/data";
        }
    }


    public static void main(String[] args) throws Exception {

        NetWork netWork = new NetWork();
        netWork.start();
        String nextLine;
        Client client = Client.getInstance();
        client.loadWallets(dataDir);
        client.loadWallet(dataDir);

        Pool.getInstance().setNetWork(netWork);
        Pool.getInstance().setWallet(client.getCurrent());

        Ledger ledger = new Ledger();

        printHelp();


        while ((nextLine = scanner.nextLine()) != null && !"quit".equals(nextLine)) {
            try {
                if (nextLine == null || nextLine.trim().length() == 0) {
                    continue;
                }

                if ("show wallet".equals(nextLine)) {

                    client.printCurrent();

                } else if (nextLine.startsWith("add wallet")) {

                    String[] items = nextLine.split(" ");
                    if (items.length != 3) {
                        System.out.println("bad parameter");
                        continue;
                    }

                    String name = items[2].trim();

                    if (name.length() == 0) {
                        System.out.println("wallet name can not be null");
                    }

                    boolean result = client.addWallet(dataDir, name);
                    if (result) {
                        System.out.println("add success");
                    } else {
                        System.out.println("add fail");
                    }

                } else if ("help".equals(nextLine)) {

                    printHelp();

                } else if ("show wallets".equals(nextLine)) {

                    client.printAll();

                } else if ("genesis block".equals(nextLine)) {

                    ledger.genesisBlock(genesisWallet(), netWork);

                } else if (nextLine.startsWith("pay")) {

                    String[] items = nextLine.split(" ");
                    String address = items[1];
                    int amount = Integer.parseInt(items[2]);
                    boolean result = client.pay(address, amount, netWork);
                    if (!result) {
                        System.out.println("支付失败");
                    }

                } else if (nextLine.equals("packNext")) {

                    Pool.getInstance().tryPackNext();

                } else if (nextLine.equals("printChain")) {

                    BlockChain.getInstance().printBlockChain();

                } else if (nextLine.equals("billFlow")) {

                    Client.getInstance().getCurrent().printInfo();

                } else if (nextLine.startsWith("switch wallet")) {

                    String[] items = nextLine.split(" ");
                    if (items.length != 3) {
                        System.err.println("参数错误");
                    }
                    boolean result = client.switchWallet(items[2]);
                    if (!result) {
                        System.out.println("切换钱包失败");
                    }
                } else {
                    printHelp();
                }
            } catch (Exception e) {
                // 保证主线程不退出
                e.printStackTrace();
            }
        }

        netWork.stop();
        System.out.println("bey");
    }


    private static void printHelp() {
        System.out.println("command list");
        System.out.println("add wallet name");
        System.out.println("show wallet");
        System.out.println("show wallets");
        System.out.println("genesis block");
        System.out.println("pay address amount");
        System.out.println("packNext");
        System.out.println("printChain");
        System.out.println("billFlow");
        System.out.println("switch wallet name1");
    }

    /**
     * 请勿修改这里的信息，会导致创世区块校验不成功
     * 如果您想修改，请同步修改NetWork的FIRST参数
     * 第一个区块头采用硬编码校验
     *
     * @return
     */

    private static Wallet genesisWallet() {
        Wallet wallet = new Wallet();
        wallet.setName("name.txt");
        wallet.setCreateTime(1659959335117L);
        wallet.setPrivateKey("CGLrpyEbjWmqGRdPfuXXmAs3YucVQkXY8DATQqR5ogPh22efVGzRifTByfkk35fjQ4hJY4LeSYezT6KyUkns5PeK5xHwhvkEwdNvRAjxBn6aq1VdKUSpmwDWTWHqLgixVaXJnXnSLiiRMNBLYx6HGqcbWEHSt7ANejJsKSg8SWmaGsqK1QZByeAVA36dzDiKNr1tAuucE6awipLeiKr3eTkYwRBtg5UUqN6sYytQyWqXgKcmYC1JNoWgags6ozNdugSazpTiZipcvxk1RBNBXjYbG2KkkTbGrwWjiYkwkGbLPdjL96hLdop6nCexbYxcLzhsA8rxCUD6dwc2GjVfRUoNHYpMeYasahyqq3sYauf9vDGZ4k7jhDh9CJoEpTgx6JoDmzyTo3GuRPejyAT2CGfnNDdfSPQyvKb1q18S6bDzrThHAVDEcwcFbqXGgo5CbmbwwTeJySwxGZ7YgiM8eD6zxwvqZ5brNvgPcMiTCAqvVoxR1EJ2U85k9AdByV1BLssDdqquErWYAT2KZuGFKwyh3mMqP2nCq9TgCHCoSAhr94ePv9fkigvfnXPbUKtv4uf5cFD6oehbFq5HbhTvNcWMhrmG8swUMmiSvF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zjP7MCVbyyCA89UW6Wvjp9HrAMRjjdU1F9BxKszuz9vTYxTX2V2aZGc4TwP7W56i4jeU3FLV3ATvccPTJQgwpTXxyZezMcUmER63e4UboUfbUhZYCVkD7d7nVR3suqfnpB7LJj45mKnbJKkWYC5KcEuh1zb51NLSiVMc97w7hXDE3D1KuNDgWTJ6J4nJVTa2DBYRkQ1AsXWPK3yX5pGVVYH8nVYTa2fSRekMys196iPqHB5hd5rguJkwdTrbNabY3VRD7FsKvYBRKdjpgGiHHUFdvZ2Dz5pbiA4ojs1eMY3EBVu3BdQkU82gM2uQyvKweoXeZrnyUdH5PBxpeX4vTuYxKznW792f5iJEBoBv4GyC92L9G6hvAjn7Gn9Dr");
        wallet.setAddress("1LU3YiMadKyRkoWjncXBbW35hhFU77RCdw");
        return wallet;
    }


}
